﻿using System.Linq;
using System.Web.Mvc;
using System.Collections.Generic;

using ASPNETMVCApplication.Code.Filters;
using ASPNETMVCApplication.Repositories;
using ASPNETMVCApplication.ActionServiceReference;
using ASPNETMVCApplication.Code.HtmlHelpers;
using ASPNETMVCApplication.Controllers;
using ASPNETWebApplication;
using ASPNETMVCApplication.Areas.Supp.Models;
using ASPNETMVCApplication.Areas.Cust.ViewModels;
using System;
using ASPNETMVCApplication.Areas.Shop.Models;

namespace ASPNETMVCApplication.Areas.Supp.Controllers
{
    /// <summary>
    /// Controller class for the Shopping area.
    /// </summary>
    public class SupplierController : BaseController
    {
        private ISupplierRepository _supplierRepository;
        private ICustomerRepository _customerRepository;
        private IOrderRepository _orderRepository;
        ITrafficRepository _trafficRepository;
        ICategoryRepository _categoryRepository;
        public SupplierController()
            : this(new CustomerRepository(), new OrderRepository(), new TrafficRepository(), new CategoryRepository(), new SupplierRepository())
        {
        }

        /// <summary>
        /// Overloaded 'injectable' Constructor for ShopController.
        ///
        /// Pattern: Constructor Dependency Injection (DI).
        /// </summary>
        /// <param name="categoryRepository"></param>
        /// <param name="productRepository"></param>
        /// <param name="cartRepository"></param>
        public SupplierController(ICustomerRepository customerRepository, IOrderRepository orderRepository, ITrafficRepository trafficRepository, ICategoryRepository categoryRepository, ISupplierRepository supplierRepository)
        {
            _customerRepository = customerRepository;
            _orderRepository = orderRepository;
            _trafficRepository = trafficRepository;
            _categoryRepository = categoryRepository;
            _supplierRepository = supplierRepository;
        }

        [Menu(MenuItem.Supplier)]
        public ActionResult Index()
        {
            if (Session["suppId"] != null)
            {
                ShowAllOrder();
                return View();
            }
            else
            {
                return RedirectToAction("Login");
            }
        }

        //logout tai khoan supplier
        [HttpGet]
        public ActionResult LogOut()
        {
            Session["suppId"] = null;
            return RedirectToAction("../../Home");
        }
        //
        public ActionResult Register()
        {
            return View();
        }
        //register account supplier
        [HttpPost]
        public ActionResult Register(SupplierModel suppModel)
        {
            if (ModelState.IsValid)
            {
                var supplier = ASPNETMVCApplication.Areas.Supp.Models.Mapper.FromModel(suppModel);
                _supplierRepository.Insert(supplier);
                var users = _supplierRepository.Login(supplier.UserName, supplier.Password);
                if (users.Count != 0)
                {
                    // ViewData["Messeage"] = "Thành công";
                    Session["suppId"] = users[0].SupplierId;
                    ViewData["Messeage"] = Session["suppId"];
                    return View();
                }
                else
                {
                    ViewData["Messeage"] = "Không thành công";
                    return View();
                }
            } 
            else
            {
                return View(suppModel);
            }
            
        }
        //Supplier login action
        public ActionResult Login()
        {
            return View();
        }
        //Supplier login 
        [HttpPost]
        public ActionResult Login(SupplierLoginModel loginModel)
        {
            if (ModelState.IsValid)
            {
                SupplierModel supplier = new SupplierModel();
                supplier.UserName = loginModel.UserName;
                supplier.Password = loginModel.Password;
                var userLogin = ASPNETMVCApplication.Areas.Supp.Models.Mapper.FromModel(supplier);
                var users = _supplierRepository.Login(userLogin.UserName, userLogin.Password);
                if (users.Count != 0)
                {
                    // ViewData["Messeage"] = "Thành công";

                    Session["suppId"] = users[0].SupplierId;
                    ShowAllOrder();
                    return RedirectToAction("SearchOrder");
                }
                else
                {
                    ViewData["Messeage"] = "Không thành công";
                    return View();
                }
            }
            else
            {
                return View(loginModel);
            }
            
            
            
        }


        //tim kiem don hang
        [HttpGet]
        public ActionResult SearchOrder()
        {

            GetAllCustomer();
            ShowAllOrder();
            return View();
           
        }
        [HttpPost]
        public ActionResult SearchOrder(OrderModel orderModel)
        {
            OrderViewModels orderViewModels = null;
            Supplier supplier = null;
            Traffic traffic = null;
            Category category = null;
            List<OrderDetail> orderDetail = null;
            int suppId = (int)Session["suppId"];
            List<OrderViewModels> listViewModel = new List<OrderViewModels>();
            var listOrder = _orderRepository.GetOrderList(orderModel.CustomerId, suppId, orderModel.DateFrom, orderModel.DateThru, 0);
            foreach (Order od in listOrder.ToList())
            {
                supplier = _supplierRepository.Get(od.SupplierId);
                orderDetail = _orderRepository.Get(od.OrderId).OrderDetails.ToList();
                category = _categoryRepository.Get(orderDetail[0].CategoryId);
                traffic = _trafficRepository.Get(category.TrafficId, od.SupplierId);
                orderViewModels = new OrderViewModels { _order = od, _listOrderDetail = orderDetail, _category = category, _supplier = supplier, _traffic = traffic };
                listViewModel.Add(orderViewModels);
            }
            GetAllCustomer();
            ViewData["listOrderNew"] = listViewModel;

            return View();
        }

        private void ShowAllOrder()
        {
           
            OrderViewModels orderViewModels = null;
            Supplier supplier = null;
            Traffic traffic = null;
            Category category = null;
            List<OrderDetail> orderDetail = null;
            int suppId = (int)Session["suppId"];
            List<OrderViewModels> listViewModel = new List<OrderViewModels>();
            var listOrder = _orderRepository.GetOrderList(0, suppId, DateTime.Parse("01/01/1990"), DateTime.Now, 0);
            foreach (Order od in listOrder.ToList())
            {
                supplier = _supplierRepository.Get(od.SupplierId);
                orderDetail = _orderRepository.Get(od.OrderId).OrderDetails.ToList();
                category = _categoryRepository.Get(orderDetail[0].CategoryId);
                traffic = _trafficRepository.Get(category.TrafficId, od.SupplierId);
                orderViewModels = new OrderViewModels { _order = od, _listOrderDetail = orderDetail, _category = category, _supplier = supplier, _traffic = traffic };
                listViewModel.Add(orderViewModels);
            }
            ViewData["listOrderNew"] = listViewModel;
        }

        private void GetAllCustomer()
        {
            List<Customer> listCust = _customerRepository.GetList(null);
            List<SelectListItem> listItem = new List<SelectListItem>();
            foreach (Customer cust in listCust)
            {
                listItem.Add(new SelectListItem { Text = cust.Name, Selected = false, Value = cust.CustomerId.ToString() });
            }
            ViewData["ListCust"] = listItem;
            List<SelectListItem> status = new List<SelectListItem>();
            status.Add(new SelectListItem { Text = "Đã thanh toán", Value = "1", Selected = true });
            status.Add(new SelectListItem { Text = "Chưa thanh toán", Value = "0", Selected = false });
            ViewData["Status"] = status;
        }
        [HttpGet]
        public void GetCustByStatus(int id)
        {

            int suppId = (int)Session["suppId"];
            List<OrderViewModels> listViewModel = new List<OrderViewModels>();
            var listOrder = _orderRepository.GetOrderList(0, suppId, DateTime.Parse("01/01/1990"), DateTime.Now, id);
            List<Customer> listCust = new List<Customer>();
            foreach (Order order in listOrder.ToList())
            {
                Customer cust = new Customer();
                cust = _customerRepository.Get(order.CustomerId);
                listCust.Add(cust);
            }
            Response.Write(" <select name='CustomerId'  >");
            foreach (Customer cust in listCust)
            {
                Response.Write(@"<option value="+cust.CustomerId+">"+cust.Name+"</option> ");
            }
            Response.Write("</select>");
        }
    }
}


